//
//  LYFCommentViewController.m
//  段子手
//
//  Created by dllo on 16/3/24.
//  Copyright © 2016年 JokesTeam. All rights reserved.
//

#import "LYFCommentViewController.h"
#import "LYFCommentModel.h"


@interface LYFCommentViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) UIView *headerView;/**< tableview的头视图 */
@property (nonatomic, retain) UIImageView *iconImg;/**< cell头像 */
@property (nonatomic, retain) UILabel *nameLabel;/**< 名字 */
@property (nonatomic, retain) UILabel *contentLabel;/**< 内容文字 */
@property (nonatomic, retain) UIImageView *bigImg;/**< 发表的图片 */
@property (nonatomic, retain) UIButton *upButton;/**< 赞图片 */
@property (nonatomic, retain) UILabel *upLabel;/**< 赞的人数 */
@property (nonatomic, retain) UIButton *downButton;/**< 不赞图片 */
@property (nonatomic, retain) UILabel *downLabel;/**< 不赞人数 */
@property (nonatomic, retain) UIButton *hotButton;/**< 评论图片 */
@property (nonatomic, retain) UILabel *hotLabel;/**< 评论人数 */
@property (nonatomic, retain) UIButton *forwardButton;/**< 转发图片 */
@property (nonatomic, retain) UILabel *forwardLabel;/**< 转发人数 */
@property (nonatomic, assign) BOOL isSelect;
@property (nonatomic, retain) NSMutableArray *dataArr;

@end

@implementation LYFCommentViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.dataArr = [NSMutableArray array];
    }
    return self;
}

- (void)dealloc {
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self.tabBarController setHidesBottomBarWhenPushed:YES];
    
    [self dataHandle];
    [self createTableView];
    [self createHeaderVeiw];
    _isSelect = NO;
}

/** 创建tableview */
- (void)createTableView {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HETGHT - 64) style:UITableViewStylePlain];
    [self.view addSubview:_tableView];
    
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
    [_tableView registerClass:[LYFCommentTableViewCell class] forCellReuseIdentifier:@"cell"];
    
    
    
    
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataArr.count;
}

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LYFCommentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.model = _dataArr[indexPath.row];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    LYFCommentModel *model = _dataArr[indexPath.row];
    NSDictionary *textDic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:17], NSFontAttributeName, nil];
    CGRect rect = [model.text boundingRectWithSize:CGSizeMake(SCREEN_WIDTH - 80, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:textDic context:nil];

    return 60 + rect.size.height;
}


/** 创建tableview头视图 */
- (void)createHeaderVeiw {
    
    NSDictionary *textDic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:15], NSFontAttributeName, nil];
    CGRect rect = [_model.text boundingRectWithSize:CGSizeMake(SCREEN_WIDTH - 20, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:textDic context:nil];
    
    self.headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 330 + rect.size.height)];
    
    _tableView.tableHeaderView = _headerView;
    
    self.iconImg = [[UIImageView alloc] init];
    _iconImg.frame = CGRectMake(10, 10, 40, 40);
    _iconImg.backgroundColor = [UIColor orangeColor];
    _iconImg.layer.cornerRadius = 20;
    _iconImg.layer.masksToBounds = YES;/**< 图片圆角吻合 */
    [_iconImg sd_setImageWithURL:[NSURL URLWithString:_model.avatar_url]];
    [self.headerView addSubview:_iconImg];
    
    self.nameLabel = [[UILabel alloc] init];
    _nameLabel.frame = CGRectMake(70, 20, 150, 30);
//    _nameLabel.backgroundColor = [UIColor colorWithRed:1.000 green:0.400 blue:0.400 alpha:1.000];
    _nameLabel.textColor = [UIColor colorWithWhite:0.298 alpha:1.000];
    _nameLabel.font = [UIFont systemFontOfSize:15];
    _nameLabel.text = _model.name;
    [self.headerView addSubview:_nameLabel];
    
    
    
    
    self.contentLabel = [[UILabel alloc] init];
    _contentLabel.frame = CGRectMake(10, 70, SCREEN_WIDTH - 20, rect.size.height);
//    _contentLabel.backgroundColor = [UIColor colorWithRed:0.400 green:1.000 blue:0.800 alpha:1.000];
    _contentLabel.font = [UIFont systemFontOfSize:15];
    _contentLabel.numberOfLines = 0;
    _contentLabel.text = _model.text;
    [self.headerView addSubview:_contentLabel];
    
    self.bigImg = [[UIImageView alloc] init];
    _bigImg.frame = CGRectMake(10, 70 + rect.size.height + 10, SCREEN_WIDTH - 20, 200);
//    _bigImg.backgroundColor = [UIColor colorWithWhite:0.400 alpha:1.000];
    [_bigImg sd_setImageWithURL:[NSURL URLWithString:_model.url]];
    [self.headerView addSubview:_bigImg];
    
    self.upButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _upButton.frame = CGRectMake(0, 70 + rect.size.height + 10 + 200 + 10, 30, 30);
    [_upButton setImage:[UIImage imageNamed:@"RC32NLI__LBBJR]ET5I%D{5.png"] forState:UIControlStateNormal];
    
    [_upButton addTarget:self action:@selector(upButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.headerView addSubview:_upButton];
    
    self.upLabel = [[UILabel alloc] init];
    _upLabel.frame = CGRectMake(40, 70 + rect.size.height + 10 + 200 + 10, 50, 30);
//    _upLabel.backgroundColor = [UIColor colorWithRed:0.502 green:0.251 blue:0.000 alpha:1.000];
    _upLabel.textColor = [UIColor colorWithWhite:0.298 alpha:1.000];
    _upLabel.font = [UIFont systemFontOfSize:15];
    _upLabel.text = _model.digg_count;
    [self.headerView addSubview:_upLabel];
    
    self.downButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _downButton.frame = CGRectMake(95, 70 + rect.size.height + 10 + 200 + 10, 30, 30);
    [_downButton setImage:[UIImage imageNamed:@"51FYKZ@K77SS}A0@{G_C]C7.png"] forState:UIControlStateNormal];
    
    [_downButton addTarget:self action:@selector(downButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.headerView addSubview:_downButton];
    
    self.downLabel = [[UILabel alloc] init];
    
    _downLabel.frame = CGRectMake(125, 70 + rect.size.height + 10 + 200 + 10, 50, 30);
//    _downLabel.backgroundColor = [UIColor colorWithRed:0.400 green:0.400 blue:1.000 alpha:1.000];
    _downLabel.textColor = [UIColor colorWithWhite:0.298 alpha:1.000];
    _downLabel.font = [UIFont systemFontOfSize:15];
    _downLabel.text = _model.bury_count;
    [self.headerView addSubview:_downLabel];
    
    self.hotButton = [UIButton buttonWithType:UIButtonTypeCustom];
    
    _hotButton.frame = CGRectMake(180, 70 + rect.size.height + 10 + 200 + 10, 30, 30);
    [_hotButton setImage:[UIImage imageNamed:@"LK@AF$6X){$X6E2WH3}5MUI.png"] forState:UIControlStateNormal];
    
    [_hotButton addTarget:self action:@selector(hotButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.headerView addSubview:_hotButton];
    
    self.hotLabel = [[UILabel alloc] init];
    
    _hotLabel.frame = CGRectMake(210, 70 + rect.size.height + 10 + 200 + 10, 50, 30);
//    _hotLabel.backgroundColor = [UIColor orangeColor];
    _hotLabel.textColor = [UIColor colorWithWhite:0.298 alpha:1.000];
    _hotLabel.font = [UIFont systemFontOfSize:15];
    _hotLabel.text = _model.comment_count;
    [self.headerView addSubview:_hotLabel];
    
    self.forwardButton = [UIButton buttonWithType:UIButtonTypeCustom];
    
    _forwardButton.frame = CGRectMake(280, 70 + rect.size.height + 10 + 200 + 10, 30, 30);
    [_forwardButton setImage:[UIImage imageNamed:@"S7YX~39TSWAV2@[[WK19UXF.png"] forState:UIControlStateNormal];
    
    [_forwardButton addTarget:self action:@selector(forwardButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.headerView addSubview:_forwardButton];
    
    self.forwardLabel = [[UILabel alloc] init];
    
    _forwardLabel.frame = CGRectMake(310, 70 + rect.size.height + 10 + 200 + 10, 50, 30);
//    _forwardLabel.backgroundColor = [UIColor colorWithRed:0.000 green:0.502 blue:0.502 alpha:1.000];
    _forwardLabel.textColor = [UIColor colorWithWhite:0.298 alpha:1.000];
    _forwardLabel.font = [UIFont systemFontOfSize:15];
    _forwardLabel.text = _model.share_count;

    [self.headerView addSubview:_forwardLabel];

}


/** 点赞方法 */
- (void)upButtonAction:(UIButton *) upButton {
    if (_isSelect == YES) {
        [_upButton setImage:[UIImage imageNamed:@"LSIJ7U8~XLPV(1HNCD9DYJO.png"] forState:UIControlStateNormal];
    } else {
        [_upButton setImage:[UIImage imageNamed:@"RC32NLI__LBBJR]ET5I%D{5.png"] forState:UIControlStateNormal];
    }
    _isSelect =!_isSelect;
}

/** 不攒方法 */
- (void)downButtonAction:(UIButton *) downButton {
    
    if (_isSelect == YES) {
        [_downButton setImage:[UIImage imageNamed:@"7Q5DXKBUXU`GULEZZ72B4XN.png"] forState:UIControlStateNormal];
    } else {
        [_downButton setImage:[UIImage imageNamed:@"51FYKZ@K77SS}A0@{G_C]C7.png"] forState:UIControlStateNormal];
    }
    
    _isSelect =!_isSelect;
    
}

/** 评论方法 */
- (void)hotButtonAction:(UIButton *) hotButton {
    
}

/** 转发方法 */
- (void)forwardButtonAction:(UIButton *) forwardButton {
    
}


/** 数据解析 */
- (void)dataHandle {
    
    NSString *url = [NSString stringWithFormat:@"http://isub.snssdk.com/neihan/comments/?iid=3884185944&os_version=9.3&os_api=18&app_name=joke_essay&channel=App Store&device_platform=iphone&idfa=F2C266F2-2A1C-4D42-85CC-07B17EE56F90&vid=68B1D0A2-5547-4C3F-B88A-D9B55797F72D&openudid=15d474a633f829aabda5b6e1420e0253418080e8&device_type=iPhone 6 Plus&version_code=5.0.1&ac=WIFI&screen_width=1242&device_id=12502863248&aid=7&count=20&device_id=12502863248&group_id=%@&offset=0&sort=hot&tag=joke", _model.group_id];
    
//    NSLog(@"%@", url);
    
    [WxlAPPTools GETWithURL:url par:nil success:^(id responseObject) {
        
        NSLog(@"%@", responseObject);
      
        NSArray *arr = [LYFCommentModel modelHandelWithArray:responseObject[@"data"][@"recent_comments"]];
//        NSLog(@"%@", arr);
        for (LYFCommentModel *model in arr) {
            [_dataArr addObject:model];
        }
        NSLog(@"%@", _dataArr);
        [_tableView reloadData];
        
        for (LYFCommentModel *model in _dataArr) {
            NSLog(@"%@", model.user_name);
        }
        
    } filed:^(NSError *error) {
        
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
